public class PrimeNumbers {
    public static void main(String[] args) {
        final int MAX = 20000;
        boolean[] isPrime = sieveOfEratosthenes(MAX);
        int count = 0;
        
        for (int i = 2; i <= MAX; i++) {
            if (isPrime[i]) {
                System.out.print(i + " ");
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
    }

    public static boolean[] sieveOfEratosthenes(int max) {
        boolean[] isPrime = new boolean[max + 1];
        for (int i = 2; i <= max; i++) {
            isPrime[i] = true;
        }

        for (int factor = 2; factor*factor <= max; factor++) {
            if (isPrime[factor]) {
                for (int j = factor*factor; j <= max; j += factor) {
                    isPrime[j] = false;
                }
            }
        }

        return isPrime;
    }
}